//
//  YMPasswordInputView.swift
//  Driver
//
//  Created by lantaiyuan on 2017/10/27.
//  Copyright © 2017年 youmy. All rights reserved.
//

import UIKit

class YMPasswordInputView: YMUIView {

    typealias finishCloure = (_ text: String)->()
    
    var finished:finishCloure?
    
    @IBOutlet var view: UIView!
    @IBOutlet weak var pwdLb0: UILabel!
    @IBOutlet weak var pwdLb1: UILabel!
    @IBOutlet weak var pwdLb2: UILabel!
    @IBOutlet weak var pwdLb3: UILabel!
    @IBOutlet weak var pwdLb4: UILabel!
    @IBOutlet weak var pwdLb5: UILabel!
    
    fileprivate var arry:Array<UILabel>? = []
    fileprivate var tempStr:String?
    
    lazy var pwdTF:UITextField = {
        let tf = UITextField()
        tf.isHidden = true
        tf.keyboardType = .numberPad
        return tf
    }()
    
    @IBAction func showKeyBoard(_ sender: UIButton) {
        pwdTF.becomeFirstResponder()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    private func setup() {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "YMPasswordInputView", bundle: bundle)
        self.view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        self.view.frame = bounds
        self.addSubview(view)
        
        self.addSubview(pwdTF)
        arry?.append(pwdLb0)
        arry?.append(pwdLb1)
        arry?.append(pwdLb2)
        arry?.append(pwdLb3)
        arry?.append(pwdLb4)
        arry?.append(pwdLb5)
        
        NotificationCenter.default.addObserver(self, selector: #selector(textFieldChange), name: NSNotification.Name.UITextFieldTextDidChange, object: pwdTF)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        var i:CGFloat = 0
        let width:CGFloat = (bounds.width) / 6.0
        let height:CGFloat = bounds.height
        for label in arry! {
            let x:CGFloat = width * i
            label.frame = CGRect(x: x, y: 0, width: width, height: height)
            i = i+1.0
        }
    }
    
    override func draw(_ rect: CGRect) {
        
        guard let context = UIGraphicsGetCurrentContext(),
            let strokeColor = UIColor(hexString: "#b4b4b4"),
            let fillColor = UIColor(hexString: "#b4b4b4")else {
                return
        }
        
        let bezirePath = UIBezierPath(roundedRect: bounds, cornerRadius: 0.0)
        bezirePath.lineWidth = 0.45
        strokeColor.setStroke()
        UIColor.white.setFill()
        bezirePath.fill()
        bezirePath.stroke()
        
        let width:CGFloat = (bounds.width) / 6.0
        let height:CGFloat = bounds.height
        for i in 1..<6 {
            let x:CGFloat = width * CGFloat(i)

            context.setStrokeColor(strokeColor.cgColor)
            context.setFillColor(fillColor.cgColor)
            context.setLineWidth(0.33)
            
            context.move(to: CGPoint(x: x, y: 0))
            context.addLine(to: CGPoint(x: x, y: height))
            context.drawPath(using: CGPathDrawingMode.fillStroke)
        }
        
        super.draw(rect)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

extension YMPasswordInputView {
    @objc fileprivate func textFieldChange(notification: Notification) {
        let textField = notification.object as! UITextField
        if let text = textField.text {
            print(text)
            let len = text.lengthOfBytes(using: .utf8) - 1
            setCircle(len)
            pwdTF.text = len > 5 ? tempStr : text
            if len == 5 {
                tempStr = text
                if finished != nil {
                    finished!(text)
                }
            }
        }
    }
    
    fileprivate func setCircle(_ count: Int){
        
        for obj in arry! {
            obj.text = obj.tag - 10000 <= count ? "⦁" : " "
        }
    }
    
    func clear(){
        pwdTF.text = nil
        setCircle(-1)
    }
}
